package com.lanou.dllo.findfood.adpter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.lanou.dllo.findfood.R;
import com.lanou.dllo.findfood.entity.MenuResult;
import com.lanou.dllo.findfood.util.Menuimage;

import com.lanou.dllo.findfood.volley.VolleySington;

import java.security.Key;
import java.util.List;

/**
 * Created by dllo on 15/10/23.
 */
public class BeautyRecylerAdpter extends RecyclerView.Adapter<BeautyRecylerAdpter.RecylerAdpter> {


   private  List<MenuResult> result;
    private VolleySington volleySington;
    private  Menuimage  mClickListener;
    public void addData(List<MenuResult> results) {
        result = results;

    }
    public  void setImageListener(Menuimage listener){

        mClickListener = listener;
    }

    public  BeautyRecylerAdpter(){

        volleySington = VolleySington.getInstance();
    }
    @Override
    public RecylerAdpter onCreateViewHolder(ViewGroup parent, int viewType) {

        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View view = inflater.inflate(R.layout.rececy_beauty_item,null);
        return new RecylerAdpter(view);
    }

    @Override
    public void onBindViewHolder(RecylerAdpter holder, int position) {
        if (result!= null && result.size() >0){
            MenuResult re = result.get(position);
            holder.titlecity.setText(re.getName());

           // holder.key.setText(re.getKeywords());

            String str=re.getKeywords();
            String[] strarray=str.split("[ ]");
            for (int i = 0; i < strarray.length; i++)
                System.out.println(strarray[i]);
            {
                String k = strarray[0];
                String k2 = strarray[1];
                String k3 = strarray[2];
                String k4 = strarray[3];
                String k5 = strarray[4];

                holder.key.setText(k);
                holder.key1.setText(k2);
                holder.key2.setText(k3);
                holder.key3.setText(k4);
                holder.key4.setText(k5);
            }

            if (re.getImg() != null && re.getImg().length() > 0) {
                String imgurl = re.getImg();
                String url = " http://tnfs.tngou.net/image/" + imgurl ;
               getBitmap(url,holder.image);
            }

        }
        holder.position = position;

    }
    private void getBitmap(String url, final NetworkImageView networkImageView) {
        ImageLoader imageLoader = volleySington.getImageLoader();
        networkImageView.setDefaultImageResId(R.mipmap.ic_launcher);
        networkImageView.setErrorImageResId(R.mipmap.ic_launcher);
        networkImageView.setImageUrl(url, imageLoader);


    }

    @Override
    public int getItemCount() {
        return result!= null && result.size()>0?result.size():0;
    }


    class RecylerAdpter extends RecyclerView.ViewHolder implements View.OnClickListener {
        private TextView titlecity,key,discreption,click,key1,key2,key3,key4;
        private  int position;

        private NetworkImageView image;

        public RecylerAdpter(View itemView) {

            super(itemView);
            titlecity = (TextView) itemView.findViewById(R.id.titlecity);
            key = (TextView) itemView.findViewById(R.id.key);
            key1= (TextView) itemView.findViewById(R.id.key1);
            key2= (TextView) itemView.findViewById(R.id.key2);
            key3= (TextView) itemView.findViewById(R.id.key3);
            key4= (TextView) itemView.findViewById(R.id.key4);
            discreption = (TextView) itemView.findViewById(R.id.discreption);
            image = (NetworkImageView) itemView.findViewById(R.id.titleC);
            click = (TextView) itemView.findViewById(R.id.click);
            click.setOnClickListener(this);
            image.setOnClickListener(this);


        }
private  int count =0;
        @Override
        public void onClick(View v) {
            switch (v.getId()){
                case R.id.click:
                    if (count==0) {
                        if (result != null && result.size() > 0) {
                            MenuResult menuResult = result.get(position);
                            String str = menuResult.getDescription();
                            discreption.setText(str);
                        }
                        count = 1;
                    }else if (count==1){
                    discreption.setText(null);
                        count = 0;
                }else {
                        discreption.setText(null);
                    }

                    break;

                case R.id.titleC :
                    if (result != null && result .size() > 0){
                        MenuResult menuResult = result.get(position);
                        if (mClickListener != null){
                            mClickListener.MenuimageOnclick(menuResult);
                        }
                    }


                    break;
                default:
                    break;

            }
        }
    }
}
